'use strict';
/**
 * @name 莫凡服务端 业务层
 * @version 0.0.1
 * @author ljr
 * @Time 2020年10月21日11:06:27
 */
const Service = require('egg').Service;

const operation_log = '[morefun:操作日志]';

class instantiationService extends Service {
  async insert(data) {
    const {
      app,
      ctx
    } = this;

    let { user_id, main_user, session, t_id, app_name, app_description, app_icon, app_alias } = data
    let template = await ctx.service.templateService.findOne(data)
    if (template.isOk) {
      let p = {
        ...template.data.template,
        ...data
      }
      const miniApp = await ctx.helper.topApi.instantiate(p)

      if (miniApp.miniapp_template_instantiate_response) {

        let params = {
          user_id, main_user, t_id,
          ...template.data.template,
          ...miniApp.miniapp_template_instantiate_response,
          status: false,
          gmt_create: new Date(),
          gmt_modified: new Date(),
        };
        console.log("xxxxx",params)
        //添加记录
        let result = await app.mysql.insert('instantiate', params);
        // if (result.insertId != 0) {
        return { isOk: 1, data: { id: result.insertId, ...params } };
      } else {
        return { isOk: 0, msg: '请重试' }
      }
    } else {
      return template
    }

  }


  async findAll(data) {
    const {
      app,
      ctx
    } = this;
    let { main_user, curPage = 1, page_size = 10 } = data
    let count = await app.mysql.query(`SELECT count(0) as count FROM instantiate where main_user=${main_user}`, '');
    let viewSql = `SELECT *,id as i_id FROM instantiate where user_id=${main_user} limit ${(curPage - 1) * page_size},${page_size}  `
    let viewLIst = await app.mysql.query(viewSql, '');

    return { isOk: 1, data: { viewLIst, curPage, page_size, pageTotal: count[0].count } };
  }

  async update(data) {
    const {
      app,
      ctx
    } = this;
    let { id, user_id, main_user, session, app_id, app_description, app_icon, app_alias, t_id, template_version } = data
    let template = await ctx.service.templateService.findOne(data)
    if (template.isOk) {
      console.log(template.data.template.template_version,"xxxxxxxxxxxxxxxxxxxxxxx",template_version)
      if(template.data.template.template_version == template_version) {
        return { isOk: 0, msg: '已经是最新版本' }
      }
      let p = {
        ...template.data.template,
        ...data
      }
      let isHaveSql = `SELECT count(0) as count FROM instantiate where id='${id}'`
      let isHave = await app.mysql.query(isHaveSql, '');
      if (isHave[0].count) {
        const miniApp = await ctx.helper.topApi.updateapp(p)
        if (miniApp.miniapp_template_updateapp_response) {
          let params = {
            ...template.data.template, main_user, id,
            ...miniApp.miniapp_template_updateapp_response
          };
  
          let updateSql = `update instantiate set app_description='${params.app_description}',status=${false},app_icon='${params.app_icon}',app_alias='${params.app_alias}',app_version='${params.app_version}',template_version='${params.template_version}',gmt_modified=now() where id= '${id}'and main_user=${main_user}`
          let result = await app.mysql.query(updateSql, '');
          // if (result.affectedRows != 0) {
          return { isOk: 1, data: result ,msg: '成功' };
        } else {
          return { isOk: 0, msg: '更新失败，请重试' }
        }

      } else {
        return { isOk: 0, msg: '数据不存在' }
      }
    } else {
      return template
    }
  }





  async delete(data) {
    const {
      app,
      ctx
    } = this;
    let { id, user_id, main_user } = data
    let isHaveSql = `SELECT count(0) as count FROM instantiate where id='${id}'`
    let isHave = await app.mysql.query(isHaveSql, '');
    if (isHave[0].count) {
      let deleteSql = `delete FROM instantiate where id= '${id}' and main_user='${main_user}' `
      let result = await app.mysql.query(deleteSql, '');
      if (result.affectedRows != 0) {
        return { isOk: 1, msg: '删除成功' };
      } else {
        return { isOk: 0, msg: '删除失败，请重试' }
      }

    } else {
      return { isOk: 0, msg: '数据不存在' }
    }
  }

  async onlineapp(data){
    const {
      app,
      ctx
    } = this;
    let { id, user_id, main_user, session, app_id, app_description, app_icon, app_alias, t_id, template_version } = data
    let template = await ctx.service.templateService.findOne(data)
    if (template.isOk) {
      let p = {
        ...template.data.template,
        ...data
      }
      let isHaveSql = `SELECT count(0) as count FROM instantiate where id='${id}'`
      let isHave = await app.mysql.query(isHaveSql, '');
      if (isHave[0].count) {
        const miniApp = await ctx.helper.topApi.onlineapp(p)
        console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
        console.log(miniApp)
        console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
        if (miniApp.miniapp_template_onlineapp_response) {
          let params = {
            ...miniApp.miniapp_template_onlineapp_response.app_info
          };
  
          let updateSql = `update instantiate set online_url='${params.online_url}',status=${true},gmt_modified=now() where id= '${id}'and main_user=${main_user}`
          let result = await app.mysql.query(updateSql, '');
          // if (result.affectedRows != 0) {
          return { isOk: 1, data: result,msg: '成功' };
        } else {
          return { isOk: 0, msg: '上线失败，请重试' }
        }

      } else {
        return { isOk: 0, msg: '数据不存在' }
      }
    } else {
      return template
    }

  }

  async testPost(user_id) {
    const {
      app,
    } = this;
    const result = {};
    result.user_id = user_id;

    app.logger.info(`${operation_log}删除了id为${user_id}的用户`);

    return result;
  }
}

module.exports = instantiationService;